package com.thfw.test.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import butterknife.ButterKnife;
import utils.AppActivities;

import com.thfw.test.MyApplication;
import com.thfw.test.utils.ConfigurationService;
import com.thfw.test.utils.ToastUtils;

/**
 * Created by csk on 2021/9/10.
 */

public abstract class BaseActivity extends AppCompatActivity {

    public enum ActivityLifecycle {
        ON_CREATE,
        ON_RESTART,
        ON_START,
        ON_RESUME,
        ON_PAUSE,
        ON_STOP,
        ON_DESTROY
    }

    protected BaseActivity context;


    protected ActivityLifecycle lifecycle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeSetContentView();
        setContentView(getLayoutId());

        //沉浸式video manager
        //红米上title会被遮住
//        StatusBarCompat.compat(this, getResources().getColor(R.color.top_bar_bg));

        ButterKnife.bind(this);

        AppActivities.instance().add(this);


        context = this;

        this.lifecycle = ActivityLifecycle.ON_CREATE;
        this.initView();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        this.lifecycle = ActivityLifecycle.ON_RESTART;
    }

    @Override
    protected void onStart() {
        super.onStart();
        this.lifecycle = ActivityLifecycle.ON_START;
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.lifecycle = ActivityLifecycle.ON_RESUME;
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.lifecycle = ActivityLifecycle.ON_PAUSE;
    }

    @Override
    protected void onStop() {
        super.onStop();
        this.lifecycle = ActivityLifecycle.ON_STOP;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        AppActivities.instance().remove(this);
        this.lifecycle = ActivityLifecycle.ON_DESTROY;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return super.onOptionsItemSelected(item);
    }

    protected void exitApplication() {

        AppActivities.instance().finishAll();
    }


    /*******************子类实现**********************/
    //获取布局文件
    public abstract int getLayoutId();

    //初始化View
    public abstract void initView();

    /**
     * 在设置ContentView前要处理的操作
     */
    protected void beforeSetContentView() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 显示Toast，一定时间内只会出现一次
     *
     * @param msg
     */
    public void showShortToast(String msg) {
        ToastUtils.showToast(this, msg);
    }

    /**
     * 显示Toast，一定时间内只会出现一次
     *
     * @param resId
     */
    public void showShortToast(int resId) {
        ToastUtils.showToast(this, resId);
    }

    public void onBack(View v) {
        finish();
    }

    public void finishAfterDelay(long delayMillis) {
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, delayMillis);
    }


    public ConfigurationService getConfig(){
        return ((MyApplication)(getApplication())).getConfigurationService();
    }
}
